#ifndef __GSM_COMMUNICATOR_H__
#define __GSM_COMMUNICATOR_H__

#include "vbb_Header.h"
#include "fw/vbb_Utils.h"

#define CONNECTION_TIMEOUT 120

#define TCP_CONNECTION_ALLOCATED        2
#define TCP_CONNECTION_CONNECTING       3
#define TCP_CONNECTION_UP               4
#define TCP_CONNECTION_CLOSING          5
#define TCP_CONNECTION_DOWN             6

#define GPRS_ATTACHING      0
#define GPRS_ATTACHED       1
#define GPRS_FAILED         2

#define COMMUNICATION_STATE_INIT                0
#define COMMUNICATION_STATE_NETWORK_REG         1
#define COMMUNICATION_STATE_GPRS_OPEN           2
#define COMMUNICATION_STATE_GPRS_WAITFOROPEN    3
#define COMMUNICATION_STATE_TCP_OPEN            4
#define COMMUNICATION_STATE_TCP_WAITFOROPEN     5
#define COMMUNICATION_STATE_DATA_PREPARE_SEND   6
#define COMMUNICATION_STATE_DATA_SENDING        7
/*#define COMMUNICATION_STATE_TCP_WAITFORCLOSE    8*/
#define COMMUNICATION_STATE_WAIT_FOR_SERVER     8
#define COMMUNICATION_STATE_TCP_RECEIVE_DATA    9
#define COMMUNICATION_STATE_TCP_CLOSE           10
#define COMMUNICATION_STATE_PROCESS_RECVDATA    11


#define BUFFER_NOT_READY    0
#define BUFFER_READY        1

bool GSM_NetworkReg(void);
char GSM_NetworkSet(void);
bool GSM_GPRS_IsOpen(void);
char GSM_GPRS_Open(void);
char GSM_GPRS_WaitForOpen(void);
void GSM_BuildDataBuffer(const char * Document, char * DataBuffer, int * DataSize, char * TmpBuffer);
bool GSM_TCP_IsOpen(void);
char GSM_TCP_Opened(const char * Document, char * DataBuffer, int * DataSize, char * TmpBuffer);
char GSM_TCP_Open(const char * ServerAddr);
char GSM_TCP_WaitForOpen(void);
char GSM_TCP_DataWaitForReady(void);
int GSM_TCP_DataPrepareSend(char * DataBuffer, int DataSize);
int GSM_TCP_DataSendData(char * DataBuffer, int SendSize);
char GSM_TCP_WaitForServerClosed(void);
char GSM_TCP_CloseClient(void);
int GSM_TCP_ReceiveData(char * ResponseBuffer);

bool GSM_Communication_Start(const char * URL, char * DataBuffer, int DataSize, char * RecvBuffer, bool NeedZip, bool NeedKeepAlive, unsigned int TimeOutSec);
bool GSM_Communication_Update(void);
bool GSM_Communication_InProcess(void);
bool GSM_Communication_LastSuccess(void);
void GSM_Communication_ProcessResponse(char * ResponseBuffer, int ResponseSize);
void GSM_Communication_ProcessedComfirm(void);

#endif

